package com.fingard.dsp.bank.directbank.bojz01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.crypto.DigestHelper;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;


public class BOJZ01Base extends DirectBase implements ITcpClient{
    protected String transCode = "";
    protected String singData = "";
    protected String time;
    /**
     *
     * @param xmlWriter  ���͵�xml����
     * @param isSign      0��ʾǩ��ʱ����ͷ�����ģ�1��ʾ���͸����е�ͷ������
     * @throws Exception
     */
    protected void writeCommonHeader(XmlTextWriter xmlWriter,String isSign) throws Exception {
        /*
    <?xml version="1.0" encoding="UTF-8" ?>
    <Message>
    <Head>
    <IBSReturnCode>��Ӧ��</IBSReturnCode>
    <IBSReturnMsg>��Ӧ��Ϣ</IBSReturnMsg>
    <TransCode>������</TransCode>
    <TransTime>�ͻ��˽���ʱ��</TransTime>
    <CifNo>��ҵ�ͻ���</CifNo>
    <Ciphertext>ǩ����Ϣ����</Ciphertext>
    <CifPwd>��ҵ�ͻ�����</TransTime>
    </Head>
    <Body>
    * */
        if (StringHelper.isNullOrEmpty(transCode)) {
            throw new Exception("δ��ֵtransCode");
        }
        if("1".equals(isSign)) {
            xmlWriter.writeStartDocByDeclaration(transCode + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        }
        else if("0".equals(isSign)){
            xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        }
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("IBSReturnCode"," ");
        xmlWriter.writeElementString("IBSReturnMsg"," ");
        xmlWriter.writeElementString("TransCode",transCode);
        xmlWriter.writeElementString("CifNo",bankFront.loginName);
        if("1".equals(isSign)) {
            xmlWriter.writeElementString("TransTime", time);
            xmlWriter.writeElementString("Ciphertext",singData);
            xmlWriter.writeElementString("CifPwd",bankFront.loginPasswd);
        }
        else if("0".equals(isSign)){
            String date = Format.DateTime14Format.format(new Date());
            time = date;
            xmlWriter.writeElementString("TransTime", time);
        }
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};
        String httpUrl = "";
        httpUrl = bankFront.serverURL;
        WriteBankLogLn("2.�ύ���еĵ�ַ��" + httpUrl);
        String ab = "<Message>";
        String ac = "</Message>";
        int ib = p_reqStr.indexOf(ab);
        int ic = p_reqStr.indexOf(ac);
        p_reqStr = p_reqStr.substring(0,ib).replace("\r\n", "")+p_reqStr.substring(ib,ic+10).replace(" ","").replace("\r\n","");
       /* p_reqStr = p_reqStr.replace("\r\n","");*/
        WriteBankLogStep2(p_reqStr);
        String value = p_reqStr.substring(p_reqStr.indexOf("<?xml"),p_reqStr.indexOf("<Ciphertext>"))+p_reqStr.substring(p_reqStr.indexOf("</CifPwd>")+9);
        /*System.out.println("���е�"+value);
        System.out.println("���е�ʵ��"+p_reqStr);
        System.out.println("����md5���ܺ�" + DigestHelper.cryptoMD5RetHexStr(value.getBytes()));*/
        //��������ǰ��
        WebRequest tmpRequest = new WebRequest(httpUrl);
        tmpRequest.setRequestProperty("accept", "text/b2exml");
        String charsetName = "UTF-8";
        byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }
    // ����ǩ���ַTCP/IPЭ�鴫��
    protected String[] sendToBankFrontSign(String signXml) throws Exception{
        String[] retStr = new String[]{"",""};

        String ab = "<Message>";
        String ac = "</Message>";
        int ib = signXml.indexOf(ab);
        int ic = signXml.indexOf(ac);

        String bodycontent = signXml.substring(0,ib).replace("\r\n", "")+signXml.substring(ib,ic+10).replace(" ","").replace("\r\n","");
        /*System.out.println("ǩ���" + bodycontent);
        System.out.println("ǩ��md5���ܺ�" + DigestHelper.cryptoMD5RetHexStr(bodycontent.getBytes()));*/
        bodycontent = DigestHelper.cryptoMD5RetHexStr(bodycontent.getBytes("UTF-8"));
        int length = bodycontent.getBytes("UTF-8").length;
        String qmhead = "POST HTTP://192.168.3.102/servlet/ABC95599.Trans HTTP/1.0\r\n" +
                "Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n" +
                "Referer: HTTP://192.168.3.102/personRACrtLY.htm\r\n" +
                "Accept-Language: zh-cn\r\n" +
                "Content-Type:  INFOSEC_SIGN/1.0\r\n" +
                "Proxy-Connection: Keep-Alive\r\n" +
                "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n" +
                "Host: 192.168.3.102\r\n" +
                "Content-Length: "+String.valueOf(length)+"\r\n" +
                "Pragma: no-cache\r\n\r\n";
        String p_reqString = qmhead+bodycontent;
        WriteBankLogLn(bankFront.serverIP+":"+bankFront.signPort);

        //��������ǰ��
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP,Integer.parseInt(bankFront.signPort),this);
        String tmpCharset = "UTF-8";
        byte[] tmpBytesBody = p_reqString.getBytes(tmpCharset);

        WriteBankLogStep2(p_reqString);
        TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesBody);
        retStr[0] = tmpReceiver.connState;
        if(tmpReceiver.recBytes != null){
            retStr[1] = new String(tmpReceiver.recBytes,0, tmpReceiver.readedLength, "GBK");
            WriteBankLogStep3(retStr[1]);
        }
        else{
            retStr[1] = tmpReceiver.getConnMsg();
        }
        if(!tmpReceiver.isCompleted){
            WriteBankLogLn(tmpReceiver.getConnMsg());
        }
        WriteBankLogLn("4.ǩ��صĽ��" + retStr[1]);
        return retStr;
    }

    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if (retCode.equals("000000")) {
                return true;
            }
        }
        return false;
    }
    protected  static String clearLineWrap(String signature){
        signature = signature.replace("\r","").replace("\n","");
        return signature;
    }
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 0) {
                p_receiver.contentLength = p_receiver.readedLength+2000;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}
